//
//  CXTabBarButton.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/26.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXTabBarButton.h"
#import "CXTabBarItem.h"

@interface CXTabBarButton()

@property (nonatomic, strong) CXTabBarItem * tabBarItem;

@end

@implementation CXTabBarButton

+(instancetype)tabBarButtonWithItem:(CXTabBarItem *)tabBarItem{
    return [[self alloc] initWithItem:tabBarItem];
}

-(instancetype)initWithItem:(CXTabBarItem *)tabBarItem{
    if (self = [super init]) {
        self.tabBarItem = tabBarItem;
    }
    return self;
}


-(void)cx_setupTabBarButtonLayout{
    self.titleLabel.backgroundColor = self.backgroundColor;
    self.imageView.backgroundColor = self.backgroundColor;
    CGSize titleSize = self.titleLabel.bounds.size;
    CGSize imageSize = self.imageView.bounds.size;
    CGFloat space = 5.0;
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, 0, titleSize.height + space, -(titleSize.width))];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height + space, -(imageSize.width), 0, 0)];
}


-(void)setTabBarItem:(CXTabBarItem *)tabBarItem{
    
    _tabBarItem = tabBarItem;
    
    NSString * normalTitle        = tabBarItem.normalTitle ? : @"";
    NSString * selectedTitle      = tabBarItem.selectedTitle ? : normalTitle;
    UIColor  * normalTitleColor   = tabBarItem.normalTitleColor? : [UIColor blackColor];
    UIColor  * selectedTitleColor = tabBarItem.selectedTitleColor? : normalTitleColor;
    UIColor  * backgroundColor    = tabBarItem.backgroundColor? : [UIColor clearColor];
    UIFont   * normalFont         = tabBarItem.normalTitleFont? : [UIFont systemFontOfSize:10];
    UIFont   * selectedFont       = tabBarItem.selectedTitleFont? : normalFont;
    NSAttributedString * normalAttributeTitle   = [[NSAttributedString alloc] initWithString:normalTitle
                                                                                  attributes:@{
                                                                                               NSForegroundColorAttributeName:normalTitleColor,
                                                                                               NSFontAttributeName:normalFont
                                                                                               }];
    NSAttributedString * selectedAttributeTitle = [[NSAttributedString alloc] initWithString:selectedTitle
                                                                                  attributes:@{
                                                                                               NSForegroundColorAttributeName:selectedTitleColor,
                                                                                               NSFontAttributeName:selectedFont
                                                                                               }];
    
    [self setBackgroundColor:backgroundColor];
    [self setImage:tabBarItem.normalImage forState:UIControlStateNormal];
    [self setImage:tabBarItem.selectedImage forState:UIControlStateSelected];
    [self setBackgroundImage:tabBarItem.normalBackgroundImage forState:UIControlStateNormal];
    [self setBackgroundImage:tabBarItem.selectedBackgroundImage forState:UIControlStateSelected];
    [self setAttributedTitle:normalAttributeTitle forState:UIControlStateNormal];
    [self setAttributedTitle:selectedAttributeTitle forState:UIControlStateSelected];
    self.titleLabel.backgroundColor = [UIColor blackColor];
    self.titleLabel.font = [UIFont systemFontOfSize:10];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)setHighlighted:(BOOL)highlighted {};

@end
